package apptoko.com;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import uk.co.senab.photoview.PhotoViewAttacher;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import apptoko.com.util.Constants;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

public class FullScreenshotImageActivity extends Activity {
	
	private ViewFlow screenShotSlide;
	private String[] imageUrls;
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_full_screenshot);
		Bundle bundle = getIntent().getExtras();
		imageUrls = bundle.getStringArray(Constants.IMAGES);
		index = bundle.getInt(Constants.IMAGE_POSITION);
		screenShotSlide = (ViewFlow) findViewById(R.id.viewflow);
		screenShotSlide.setAdapter(new ScreenShotSlideAdapter(this), index);
		CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
		screenShotSlide.setFlowIndicator(indic);
		EasyTracker.getInstance().setContext(this);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		screenShotSlide.onConfigurationChanged(newConfig);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	private class ScreenShotSlideAdapter extends BaseAdapter {
		
		private LayoutInflater inflater;
		
		public ScreenShotSlideAdapter(Context context){
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return imageUrls.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return imageUrls[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.full_image_item, null);
				holder.image = (ImageView) convertView.findViewById(R.id.full_image);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final PhotoViewAttacher attacher = new PhotoViewAttacher(holder.image);
			AQuery aq = new AQuery(convertView);
			Bitmap preset = aq.getCachedImage(imageUrls[position]);
			aq.id(holder.image).image(imageUrls[position], false, false, 0, 0, preset, AQuery.FADE_IN);
			attacher.update();
			return convertView;
		}
	}
	
	private static class ViewHolder {
		ImageView image;
	}
}
